package org.cbyyx.tc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cbyyx.tc.mapper.ProductCategoryMapper;
import org.cbyyx.tc.model.dto.categoryRequest.CategoryAddRequest;
import org.cbyyx.tc.model.dto.categoryRequest.CategoryQueryRequest;
import org.cbyyx.tc.model.dto.categoryRequest.CategoryUpdateRequest;
import org.cbyyx.tc.model.entity.ProductCategory;
import org.cbyyx.tc.model.vo.categoryResponse.CategoryVO;
import org.cbyyx.tc.service.ProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author cbyyx
* @description 针对表【product_category(商品分类表)】的数据库操作Service实现
* @createDate 2025-04-30 13:42:12
*/
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>
    implements ProductCategoryService {

    @Override
    public boolean addCategory(CategoryAddRequest request) {
        ProductCategory category = new ProductCategory();
        BeanUtils.copyProperties(request, category);
        return this.save(category);
    }

    @Override
    public boolean updateCategory(CategoryUpdateRequest request) {
        ProductCategory category = new ProductCategory();
        BeanUtils.copyProperties(request, category);
        return this.updateById(category);
    }

    @Override
    public boolean deleteCategory(Long id) {
        return this.removeById(id);
    }

    @Override
    public CategoryVO getCategory(Long id) {
        ProductCategory category = this.getById(id);
        if (category == null) {
            return null;
        }
        CategoryVO categoryVO = new CategoryVO();
        BeanUtils.copyProperties(category, categoryVO);
        return categoryVO;
    }

    @Override
    public List<CategoryVO> listCategories(CategoryQueryRequest request) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(request.getParentId() != null, ProductCategory::getParentId, request.getParentId())
               .like(StringUtils.hasText(request.getName()), ProductCategory::getName, request.getName())
               .eq(ProductCategory::getStatus, 1)
               .orderByAsc(ProductCategory::getSort);

        List<ProductCategory> list = this.list(wrapper);

        return list.stream()
            .map(category -> {
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                return categoryVO;
            })
            .collect(Collectors.toList());
    }

    @Override
    public List<CategoryVO> listChildCategories(Long parentId) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductCategory::getParentId, parentId)
               .orderByAsc(ProductCategory::getSort);
               
        List<ProductCategory> list = this.list(wrapper);

        return list.stream()
           .map(category -> {
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                return categoryVO;
            })
           .collect(Collectors.toList());
    }
}




